/* 
 * Copyright (C) 2015 fede
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package Control;

import Entity.Event;
import Entity.NotifyLink;
import Entity.RegisteredUser;
import java.util.Date;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author fede
 */
@Stateless
public class NotifyLinkFacade extends AbstractFacade<NotifyLink> {

    @PersistenceContext(unitName = "meteocal")
    private EntityManager em;

    @EJB
    private RegisteredUserFacade ruf;

    /**
     * get entity manager
     *
     * @return entity manager of the class
     */
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    /**
     * constructor
     */
    public NotifyLinkFacade() {
        super(NotifyLink.class);
    }

    /**
     * This method create the notify template for the 3 day notify
     *
     * @param event Event for notify
     * @param suggestedDate suggested date to add
     */
    public void createNotify3Day(Event event, Date suggestedDate) {
        NotifyLink toAdd = new NotifyLink();
        toAdd.setEvent(event);
        toAdd.setUser(ruf.getLoggedUser());
        toAdd.setMessage("The event " + event.getTitle() + " scheduled for "
                + event.getStartEventDate() + " has a bad weather condition!!!"
                + "\n do you want to change date with the first sunny date on: "
                + "" + suggestedDate + " ?");
        toAdd.setSuggestedDate(suggestedDate);
        em.persist(toAdd);

    }

    /**
     * This method create the notify template for the 1 day notify
     *
     * @param event Event for notify
     */
    public void createNotify1Day(Event event) {
        NotifyLink toAdd = new NotifyLink();
        toAdd.setEvent(event);
        toAdd.setUser(ruf.getLoggedUser());
        toAdd.setMessage("The event " + event.getTitle() + " scheduled for "
                + event.getStartEventDate() + " has a bad weather condition!!!");
        em.persist(toAdd);

    }

    /**
     * This method return true if Event has notify
     *
     * @param event to check
     * @param user ther user to check
     * @return True if has notify
     */
    public Boolean hasNotify(Event event, RegisteredUser user) {
        TypedQuery<NotifyLink> query = em.createNamedQuery(NotifyLink.FIND_ALL_BY_USER_AND_EVENT, NotifyLink.class);
        query.setParameter("eventId", event.getId());
        query.setParameter("userX", user.getEmail());
        return query.getResultList().size() > 0;
    }

}
